package com.smartboot.plus.plugins;


import com.smartboot.plus.holder.BrokerContextHolder;
import com.smartboot.plus.plugins.api.FeatApiPlugin;
import com.smartboot.plus.plugins.proxy.MqttProxyProtocolPlugin;
import tech.smartboot.feat.core.common.logging.Logger;
import tech.smartboot.feat.core.common.logging.LoggerFactory;
import tech.smartboot.mqtt.plugin.spec.BrokerContext;
import tech.smartboot.mqtt.plugin.spec.Options;
import tech.smartboot.mqtt.plugin.spec.Plugin;

import java.util.ArrayList;
import java.util.List;




public class FeaturesPlugin extends Plugin {
    private static final Logger LOGGER = LoggerFactory.getLogger(FeaturesPlugin.class);
    private final List<Feature> features = new ArrayList<>();


    @Override
    protected void initPlugin(BrokerContext brokerContext) throws Throwable {
        BrokerContextHolder.set(brokerContext);

        PluginConfig pluginConfig = loadPluginConfig(PluginConfig.class);

        features.add(new MqttProxyProtocolPlugin(brokerContext));
        features.add(new FeatApiPlugin(brokerContext, pluginConfig));


        for (Feature feature : features) {
            if (feature.isEnable()) {
                LOGGER.info("start feature:[{}]", feature.name());
                feature.start();
            }
        }
    }


    @Override
    protected void destroyPlugin() {
        features.forEach(Feature::destroy);
    }


    @Override
    public int order() {
        return super.order() + 1;
    }

    @Override
    public String getVersion() {
        return Options.VERSION;
    }

    @Override
    public String getVendor() {
        return Options.VENDOR;
    }

    @Override
    public String pluginName() {
        return "smart-mqtt-plus";
    }
}
